package net.lzzy.carmax;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.Function2;
import org.apache.spark.api.java.function.PairFunction;
import scala.Tuple2;



public class SparkCarMax {
    public static void main(String[] args) {
        SparkConf conf = new SparkConf().setAppName("SparkCarMax").setMaster("local[*]");
        JavaSparkContext jsc = new JavaSparkContext(conf);
        JavaRDD<String> javaRDD = jsc.textFile("src/trip2.csv");
        JavaPairRDD<String, String> sjp = javaRDD.mapToPair(new PairFunction<String, String, String>() {
            @Override
            public Tuple2<String, String> call(String s) throws Exception {
                return new Tuple2<>(s.split(",")[0], s);
            }
        });
        JavaPairRDD<String, String> str = sjp.reduceByKey(new Function2<String, String, String>() {
            @Override
            public String call(String v1, String v2) throws Exception {
                Integer vs1 = Integer.parseInt(v1.split(",")[3]);
                Integer vs2 = Integer.parseInt(v2.split(",")[3]);
                return vs1 >= vs2 ? v1 : v2;
            }
        });
        str.collect().forEach(str2 -> System.out.println(str2._2));
    }
}

